package com.rhb.ca.web.servlet;

import com.rhb.ca.web.util.Constant;
import com.rhb.ca.web.online.OnLine;
import com.rhb.ca.web.online.OnLines;
import com.rhb.ca.uzer.bean.Uzer;
import com.rhb.ca.uzer.business.UzerBusiness;
import com.rhb.ca.AppContext;

import java.io.*;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class CaService extends HttpServlet{
    private OnLines onlines;

    public CaService(){
		onlines = OnLines.getInstance();
    }

    public void doGet(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
        doPost(req, res);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException{
        String sessionid = req.getParameter(Constant.SESSIONID);
        String casaction = req.getParameter(Constant.CAS_ACTION);
        if(sessionid == null) sessionid="";
        if(casaction == null) casaction="";
        
        sessionid = sessionid.trim();
        casaction = casaction.trim();
        if(casaction.equals(Constant.CAS_GETUZER)){
            doGetUzer(req, res, sessionid);
            return;
        }

        if(casaction.equals(Constant.CAS_DOLOGIN)){
            doLogin(req, res, sessionid);
            return;
        }
        if(casaction.equals(Constant.CAS_DOLOGOUT)){
            doLogout(req, res, sessionid);
            return;
        }

        return;
    }

    public void doGetUzer(HttpServletRequest req, HttpServletResponse res, String sessionid)
        throws ServletException, IOException
    {
	    Uzer uzer = onlines.getUzer(sessionid);
	
	    OnLine ol = new OnLine();
	    ol.setSessionID(sessionid);
	    ol.setTouchIP(req.getParameter(Constant.TOUCH_IP));
	    ol.setTouchUrl(req.getParameter(Constant.TOUCH_URL));
		synchronized(onlines){
		    onlines.touchBy(ol);
		}
	    try{
	        Object obj = uzer;
	        res.setContentType("application/octet-stream");
	        ObjectOutputStream oos = new ObjectOutputStream(res.getOutputStream());
	        oos.writeObject(obj);
	        oos.close();
	    }catch(Exception e){
	        e.printStackTrace();
	    }
    }
    
    public void doLogin(HttpServletRequest req, HttpServletResponse res, String sessionid)
        throws ServletException, IOException
    {
        String loginName = req.getParameter(Constant.LOGIN_FIELD_UZER);
        String password = req.getParameter(Constant.LOGIN_FIELD_PASS);
        Uzer uzer = null;
        boolean isUzer = false;
        try{
        	UzerBusiness ub = (UzerBusiness)AppContext.getInstance().getAppContext().getBean("uzerService");
        	uzer = ub.selectUzerByLoginName(loginName);

            if(uzer != null)
                isUzer = uzer.passwordEquals(password);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        if(isUzer){
            OnLine ol = new OnLine();
            ol.setSessionID(sessionid);
            ol.setTouchIP(req.getParameter(Constant.TOUCH_IP));
            ol.setTouchUrl(req.getParameter(Constant.TOUCH_URL));
    		synchronized(onlines){
            	onlines.addOnLine(ol, uzer);
        	}
            try{
                Object obj = uzer;
                res.setContentType("application/octet-stream");
                ObjectOutputStream oos = new ObjectOutputStream(res.getOutputStream());
                oos.writeObject(obj);
                oos.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    public void doLogout(HttpServletRequest req, HttpServletResponse res, String sessionid)
        throws ServletException, IOException
    {
    	synchronized(onlines){
	        onlines.removeOnLine(sessionid);
	    }
    }

    public void init(ServletConfig cfg) throws ServletException{super.init(cfg);}
    public void destroy(){super.destroy();}
}
